package com.example.safeassistan.home.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.cy.tablayoutniubility.FragPageAdapterVpNoScroll;
import com.cy.tablayoutniubility.TabAdapterNoScroll;
import com.cy.tablayoutniubility.TabLayoutNoScroll;
import com.cy.tablayoutniubility.TabMediatorVpNoScroll;
import com.cy.tablayoutniubility.TabNoScrollViewHolder;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.example.safeassistan.home.fragment.BackLogFragment;
import com.example.safeassistan.home.fragment.CheckRecordFragment;
import com.example.safeassistan.utile.EventBusBean;
import com.example.safeassistan.utile.WeekUtils;
import com.google.android.material.tabs.TabLayout;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class CheckRecord extends BaseActivity implements View.OnClickListener {
    private ImageView ivBack;
    private ViewPager viewpager;
    private TextView tvDate,tvLastDay,tvNextDay;
    private LinearLayout llDate;
    private int thismonth=0;
    private int thisYear=0;
    private int  thisday=0;
    private String thisWeekDay="";

    private TabLayout tabLayout;
    private List<Fragment> fragmentList;
    private List<String> mTitleList; //Tab的标题
    private MainAdapter adapter;
    public static void actionStart(Context context) {
        Intent intent = new Intent(context, CheckRecord.class);
        context.startActivity(intent);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.llDate:
                TimePickerView startTime = new TimePickerBuilder(CheckRecord.this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {//选中事件回调
                        Calendar calendar=Calendar.getInstance();
                        calendar.setTime(date);
                        compute(calendar);
                    }
                })
                        .setType(new boolean[]{true, true, true, false, false, false})// 默认全部显示
                        .setCancelText("取消")//取消按钮文字
                        .isDialog(true)
                        .setSubmitText("确定")//确认按钮文字
                        .setTitleSize(18)//标题文字大小
                        .setTitleColor(getResources().getColor(R.color.white))
                        .setTitleText("检查日期")
                        .setContentTextSize(17)
                        .setOutSideCancelable(true)//点击屏幕，点在控件外部范围时，是否取消显示
                        .isCyclic(true)//是否循环滚动
                        .setTextColorOut(getResources().getColor(R.color.gray6))
                        .setTextColorCenter(getResources().getColor(R.color.blue_3B5BEA))
                        .setSubmitColor(getResources().getColor(R.color.white))//确定按钮文字颜色
                        .setLabel("年", "月", "日", "时", "分", "秒")//默认设���为年月日时分秒
                        .isCenterLabel(false) //是否只显示中间选中项的 label 文字，false 则每项 item 全部都带有 label。
                        .build();
                startTime.show();
                break;
            case R.id.tvNextDay:
                //后一天
               compute(WeekUtils.getdate(thisYear+"-"+thismonth+"-"+thisday,1));
                break;
            case R.id.tvLastDay:
                //前一天
                compute(WeekUtils.getdate(thisYear+"-"+thismonth+"-"+thisday,-1));
                break;
            case R.id.ivBack:
                finish();
                break;
        }
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.checkrecord;
    }

    @Override
    protected void initView() {
        tvDate=findViewById(R.id.tvDate);
        viewpager=findViewById(R.id.viewpager);
        tvNextDay=findViewById(R.id.tvNextDay);
        tvLastDay=findViewById(R.id.tvLastDay);
        tabLayout=findViewById(R.id.tabLayout);

        llDate=findViewById(R.id.llDate);
        ivBack=findViewById(R.id.ivBack);
        ivBack.setOnClickListener(this::onClick);
        llDate.setOnClickListener(this::onClick);
        tvNextDay.setOnClickListener(this::onClick);
        tvLastDay.setOnClickListener(this::onClick);

        fragmentList=new ArrayList<>();
        fragmentList.add(CheckRecordFragment.newInstance("0"));
        fragmentList.add(CheckRecordFragment.newInstance("1"));
        mTitleList=new ArrayList<>();
        mTitleList.add("我的");
        mTitleList.add("部门");

        adapter=new MainAdapter(getSupportFragmentManager());
        viewpager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewpager);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewpager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) { }

            @Override
            public void onTabReselected(TabLayout.Tab tab) { }
        });

        compute(Calendar.getInstance());
    }

    //适配器
    public class MainAdapter extends FragmentPagerAdapter {

        public MainAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        //获取标题
        @Override
        public CharSequence getPageTitle(int position) {
            return mTitleList.get(position);
        }
    }

    public void compute(Calendar calendar){
        thisYear=calendar.get(Calendar.YEAR);
        thismonth=calendar.get(Calendar.MONTH)+1;
        thisday=calendar.get(Calendar.DAY_OF_MONTH);
        thisWeekDay=WeekUtils.getWeek(thisYear+"-"+thismonth+"-"+thisday);
        tvDate.setText(thismonth+"月" +thisday+"日" + thisWeekDay);
        SimpleDateFormat sp=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        sentBrodcast(sp.format(calendar.getTime()));
    }

    public void sentBrodcast(String msg){
        EventBusBean eventBus=new EventBusBean();
        eventBus.setCode(100);
        eventBus.setMsg(msg);
        EventBus.getDefault().post(eventBus);
    }

    @Override
    protected void initData() { }

    @Override
    protected int getTitleBarType() {
        return TITLEBAR_NULL;
    }
}